package tedu.rocketmq.controller;

import java.util.Date;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import tedu.rocketmq.service.HelloService;

@RestController
@Slf4j
public class HelloController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @GetMapping("/send")
    public String sayHi(String name){
        //1. 组织一个消息对象message payLoad负载，就是消息body内容，但是不需要做序列化
        //payLoad可以放任何对象 默认 string会使用getBytes做序列化 默认对象使用json字符串
        Message<String> message = MessageBuilder.withPayload(name).build();
        //2. 根据需求 发送消息 同步 异步 延迟
        //同步发送 SendResult result = rocketMQTemplate.syncSend("test-topic01", message);
        //延迟同步发送 4级别 30秒，发送消息后，rocketmq队列显示消息 30之后
        //log.info("发送消息:{}",name);
        //rocketMQTemplate.syncSend("test-topic01", message, 1000, 4);
        //异步发送 非延迟
        rocketMQTemplate.asyncSend("test-topic01", message, new SendCallback() {
            //异步线程接收rocketmq反馈发送成功 调用
            @Override public void onSuccess(SendResult result) {
                log.info("发送成功:{}",result);
            }
            //异步线程接收rocketmq反馈发送失败 调用
            @Override public void onException(Throwable throwable) {
                log.info("发送失败:{}",throwable);
            }
        });
        log.info("发送消息成功:{}",name);
        return "success";
    }
    /*@GetMapping("/get")
    public String get(){
        //拉取消息方法，每次执行都会从目标主题
        List<String> receive = rocketMQTemplate.receive(String.class);

    }*/
}
